package com.css.gdzj.gdzjbxgd.modules.exportWord;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * @author Mr.Jin
 * @create 2021-01-11 14:32
 */
@Controller
public class DownloadWord {
    @RequestMapping(value="/testDownload",method=RequestMethod.GET)
    public void testDownload(HttpServletResponse resp) throws IOException{
        File file = new File("/data/word/yhsc.doc");
        resp.setHeader("Content-Type", "application/octet-stream;charset=utf-8");
        resp.setContentType("application/octet-stream");
        resp.setHeader("Content-Disposition", "attachment;filename=" + file.getName());
        byte[] buff = new byte[1024];
        BufferedInputStream bis = null;
        OutputStream os = null;
        try {
        os = resp.getOutputStream();
        bis = new BufferedInputStream(new FileInputStream(file));
        int i = bis.read(buff);
        while (i != -1) {
        os.write(buff, 0, buff.length);
        os.flush();
        i = bis.read(buff);
        }
        } catch (IOException e) {
        e.printStackTrace();
        } finally {
        if (bis != null) {
        try {
        bis.close();
        } catch (IOException e) {
        e.printStackTrace();
        }
        }
        }

    }

}
